package com.tony.business.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author x
 */
@Component
@EnableSwagger2
@ConditionalOnProperty(name = "system.swagger.enable", havingValue = "true")
public class Swagger2Config {

    @Bean
    public Docket createDefaultRestApi() {
        List<Parameter> pics = new ArrayList<>();

        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("加密串")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pics.add(tokenPar.build());

        /*ParameterBuilder timestampPar = new ParameterBuilder();
        timestampPar.name("timestamp").description("时间戳")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pics.add(timestampPar.build());*/

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("项目文档")
                //最终调用接口后会和paths拼接在一起
                .pathMapping("/")
                .apiInfo(apiInfo())
                .select()
                //控制显示的类
                .apis(RequestHandlerSelectors.basePackage("com.tony.business.controller"))
                //控制显示的类
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pics);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .description("restful风格")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    /**
     * 多个文档分组显示
     *
     * @return 文档
     */
    @Bean
    public Docket createNewApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("对接文档")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tony.business.docking.controller"))
                .paths(PathSelectors.any())
                .build();
    }

}